<?php

declare(strict_types=1);

namespace App\Orchid\Filters;

use Illuminate\Database\Eloquent\Builder;
use Orchid\Filters\Filter;
use Orchid\Screen\Fields\Select;

class PostStatusFilter extends Filter
{
    /**
     * The displayable name of the filter.
     *
     * @return string
     */
    public function name(): string
    {
        return '状态';
    }

    /**
     * The array of matched parameters.
     *
     * @return array
     */
    public function parameters(): array
    {
        return ['status'];
    }

    /**
     * Apply to a given Eloquent query builder.
     *
     * @param Builder $builder
     *
     * @return Builder
     */
    public function run(Builder $builder): Builder
    {
        $status = $this->request->get('status');
        
        if (empty($status)) {
            return $builder;
        }
        
        return $builder->where('status', $status);
    }

    /**
     * Get the display fields.
     */
    public function display(): array
    {
        return [
            Select::make('status')
                ->options([
                    'published' => '已发布',
                    'draft' => '草稿',
                    'archived' => '已归档',
                ])
                ->empty('全部状态')
                ->value($this->request->get('status'))
                ->title('状态'),
        ];
    }

    /**
     * Value to be displayed
     */
    public function value(): string
    {
        $statusMap = [
            'published' => '已发布',
            'draft' => '草稿',
            'archived' => '已归档',
        ];
        
        $status = $this->request->get('status');
        return $this->name() . ': ' . ($statusMap[$status] ?? '');
    }
}